package com.ym521.imgcrop;

import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.ym521.imgcrop.base.CropRequest;
import com.ym521.imgcrop.base.CropResultCallBack;
import com.ym521.imgcrop.base.CropType;
import com.ym521.imgcrop.callback.CropActivityResult;
import com.ym521.imgcrop.widget.RectCropView;

/**
 * @author Ym
 * E-mail: 2435970206@qq.com
 * createTime:2023/12/29
 * explain:
 */
public class ImageCrop {
    private ActivityResultLauncher<CropRequest> launcher;

    private static ImageCrop imageCrop;

    private ImageCrop() {
    }

    public static ImageCrop getInstance() {
        if (imageCrop == null)
            synchronized (ImageCrop.class) {
                if (imageCrop == null)
                    imageCrop = new ImageCrop();
            }
        return imageCrop;
    }

    public ImageCrop initCrop(FragmentActivity activity, CropResultCallBack callBack) {
        launcher = activity.registerForActivityResult(new CropActivityResult(), result -> {
            if (callBack != null) {
                callBack.onResult(result);
            }
        });
        //避免 静态持有launcher 造成内存泄漏
        activity.getLifecycle().addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    launcher = null;
                    activity.getLifecycle().removeObserver(this);
                }
            }
        });
        return this;
    }

    public void startCrop(CropRequest cropRequest) {
        if (launcher != null) {
            launcher.launch(cropRequest);
        }
    }

    /**
     * @param draRes
     * @param whRatio  裁切框的宽高比 例如 1:1 3:4 9:16
     * @param cropType
     */
    public void startCrop(@DrawableRes int draRes, RectCropView.WHRatio whRatio, CropType cropType, String extra) {
        startCrop(new CropRequest(whRatio, 0.7f, draRes, cropType, extra));
    }


    public void startCrop(@DrawableRes int draRes, CropType cropType, String extra) {
        startCrop(draRes, RectCropView.WHRatio.WH1_1, cropType, extra);
    }

    public void startCrop(@DrawableRes int draRes, CropType cropType) {
        startCrop(draRes, RectCropView.WHRatio.WH1_1, cropType, null);
    }


    /**
     * @param imgPath
     * @param cropType
     * @param whRatio  裁切框的宽高比 例如 1:1 3:4 9:16
     */
    public void startCrop(String imgPath, RectCropView.WHRatio whRatio, CropType cropType, String extra) {
        startCrop(new CropRequest(whRatio, 0.7f, imgPath, cropType, extra));
    }


    public void startCrop(String imgPath, CropType cropType, String extra) {
        startCrop(imgPath, RectCropView.WHRatio.WH1_1, cropType, extra);
    }

    public void startCrop(String imgPath, RectCropView.WHRatio whRatio, CropType cropType) {
        startCrop(imgPath, whRatio, cropType, null);
    }

    public void startSelectCrop(String imgPath, RectCropView.WHRatio whRatio, float widthRatio) {
        startCrop(new CropRequest(whRatio, widthRatio, imgPath, CropType.SELECT_RECT_CROP, null));
    }

    public void startSelectCrop(String imgPath) {
        startCrop(new CropRequest(RectCropView.WHRatio.WH1_1, 0.7f, imgPath, CropType.SELECT_RECT_CROP, null));
    }

    public void startSelectCrop(@DrawableRes int draRes, float widthRatio) {
        startCrop(new CropRequest(RectCropView.WHRatio.WH1_1, widthRatio,draRes, CropType.SELECT_RECT_CROP, null));
    }

}
